/**
 * Created by gaoyj on 2018/8/2.
 */
var myDate = new Date();
var month = myDate.getMonth() + 1;
var day = myDate.getDate();
if (month < 10) {
    month = "0" + month;
}
if (day < 10) {
    day = "0" + day;
}
var detailId = [];
var _start = myDate.getFullYear() + '-' + month + '-' + day + " 00:00:00";
var _end = myDate.getFullYear() + '-' + month + '-' + day + " 23:59:59";
$("#buyStartTime").val(_start);
$("#buyEndTime").val(_end);
laydate.render({
    elem: '#buyStartTime',
    type: 'datetime'

});
laydate.render({
    elem: '#buyEndTime'
    , type: 'datetime'
});
$('#dataTable').bootstrapTable({
    url: '/WorkNumberOrder/queryWorkNumberOrder',
    height: $(window).height() - 200,
    pageSize: 100,
    pagination: true,
    pageList: [30, 50, 80,100],
    clickToSelect: true,
    queryParams: queryParams,
    uniqueId: 'id',
    queryParamsType: '',
    sidePagination: 'server',
    responseHandler: function (res) {
        return {
            "total": res.total,//总页数
            "rows": res.resultList  //数据
        };
    },
    onClickRow: function (row) {
        if (row.userAccount != null && row.userAccount != '') {
            $("#account_field"+row.id).html(row.userAccount);
        }
    },
    columns: [[
        {
            checkbox: true
        }, {
            width: '3px',
            align: 'center',
            formatter: function (v, row, index) {
                return index + 1
            }
        },
        {
            field: 'orderId',
            title: '订单号',
            width: 150,
            align: 'center'
        },
        {
            field: 'userAccount',
            title: '会员编号/名称',
            width: 100,
            align: 'center',
            formatter:function(v,o){
                return copyAddon(v) + userAccountHidden(o.userAccount,o.id)+"</br>"+o.userName;
            }
        },
        {
            field: 'workAccount',
            title: '工号',
            width: 80,
            align: 'center'
        },
        {
            field: 'chargeAccount',
            title: '充值工号',
            width: 80,
            align: 'center',
            formatter:function(v,o){
            if(o.detail){
                return o.detail.chargeAccount;
            }
            return "-";
            }
        },
        {
            field: 'productType',
            title: '运营商',
            width: 100,
            align: 'center',
            formatter: function (v, o) {
            return productTypeConvert(o.productType);
            }
        },
        {
            field: 'money',
            title: '转账金额',
            width: 60,
            align: 'center',
            sortable: true
        },
        {
            field: 'state',
            title: '状态',
            width: 60,
            align: 'center',
            formatter: function (v) {
            return {0: "处理中", 1: "处理完成"}[v]
        }
        },
        {
            field: 'cost',
            title: '供货折扣',
            width: 60,
            align: 'center'},
        {
            field: 'time',
            title: '时间',
            width: 190,
            align: 'center',
            formatter: function (v, o) {
            if (!o.startTime)
                return "";
            return "开始:" + o.startTime + "<br/>" + "结束:" +
                o.endTime
        }
        },
        {
            field: 'settlePrice',
            title: '结算金额',
            width: 55,
            align: 'center'
        },
        {
            field: 'detail_state',
            title: '明细状态',
            width: 40,
            align: 'center',
            formatter: function (v, o) {
                if (o.detail) {
                    return {1: "成功", 2: "失败", 3: "异常",4:"待提交",5:"处理中"}[o.detail.state]
                }
            }
        },
        {
            field: 'detail_money',
            title: '明细金额',
            width: 55,
            align: 'center',
            formatter: function (v, o) {
                if (o.detail) {
                    return o.detail.money;
                }
            }
        },
        {
            field: 'detail_Time', title: '处理时间', width: 90, align: 'center', formatter: function (v, o) {
            if (o.detail) {
                if (o.detail.dealTime) {
                    return o.detail.dealTime
                }
                return "";
            }
        }
        },
        {
            field: 'detail_remark', title: '备注', width: 110, align: 'center', formatter: function (v, o) {
            if (o.detail && o.detail.remark != "null") {
                return o.detail.remark;
            }
        }
        }
    ]]
});
$("#search").click(function () {
    var timeAdd = new Date($("#buyStartTime").val());
    var timeEnd = new Date($("#buyEndTime").val());
    if (timeAdd > timeEnd) {
        layer.msg("开始时间不能大于结束时间");
        return false;
    }
    var require = (timeEnd - timeAdd) / (1 * 24 * 60 * 60 * 1000);
    if (require > 7) {
        layer.msg("时间间隔不能超过7天");
        return;
    }
    $("#dataTable").bootstrapTable("refresh");
});
function queryParams(params) {
    return {
        userName: $.trim($('#userName').val()),
        orderId: $.trim($('#orderId').val()),
        page: params.pageNumber,
        size: params.pageSize,
        startTime: $("#buyStartTime").val(),
        endTime: $("#buyEndTime").val(),
        'detail.chargeAccount': $.trim($("#chargeAccount").val()),
        state: $("#state").val(),
    };
}

//批次失败 卡号失败 批次再次提交
function operateFun(v) {
    detailId = [];
    var rows = $('#dataTable').bootstrapTable("getSelections");
    if (rows == null || rows.length === 0) {
        layer.msg("请选择要操作的数据");
        return;
    }
    for (var i = 0; i < rows.length; i++) {
        detailId.push(rows[i].detailId);
    }
    var title = "";
    switch (v) {
        case 1:
            title = "成功";
            break;
        case 2:
            title = "失败";
            break;
    }
    $.confirm({
        title: '确认!',
        content: '确定' + title + "？",
        confirm: function () {
            $.ajax({
                type: "POST",
                url: "/WorkNumberOrder/changeStatus",
                traditional: true,
                data: {
                    detailId: detailId,
                    state: v
                },
                dataType: "json",
                success: function (data) {
                    if (data.success) {
                        $("#dataTable").bootstrapTable("refresh");
                        layer.msg(data.message);
                    } else {
                        layer.msg(data.message)
                    }
                },
                beforeSend: function () {
                }
            });
        },
        cancel: function () {

        }
    });
}